package com.example.medicine.controller;



import com.example.medicine.Bean.yonghu;
import com.example.medicine.Mapper.yonghuMapper;
import com.example.medicine.md5_guid.md5_guid;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;

@Controller
public class LoginController {
    @Autowired
    private yonghuMapper yonghuMapper;

    /**
     * 展示登录界面
     * @return
     */
    @RequestMapping("/login")
    public String showLogin(){
        return "pages/login";
    }


    /**
     * 接收登录请求，进行判断
     * @param userId
     * @param pwd
     * @param model
     * @return
     */
    @PostMapping(value = "/loginIn")
    public String login(@RequestParam("userId") String userId, @RequestParam("password") String pwd, Model model,  HttpSession session) {
//        System.out.println("用户ID：" + userId);
//        System.out.println("密码：" + pwd);
        String getpwd = null;
        yonghu user = yonghuMapper.getPasswordByUserid(userId);
        String  teamname=yonghuMapper.getById(userId).getTeamName();
//        String  teamname=yonghuMapper.getById(userId).getUsername();
        if (user != null) {
            getpwd = user.getPassword();
        }
        String md5Pwd=new md5_guid(pwd).md5();
        // 对密码进行比较
        if (getpwd == null || !getpwd.equals(md5Pwd)) {
            model.addAttribute("message", 1); // 设置错误信息
            return "pages/login";   //账密错误
        } else {
            session.setAttribute("sessionUser",user);
            model.addAttribute("teamname", teamname);
            model.addAttribute("userid", userId);

            return "/home";
        }
    }



}
